package com.jh3502.helper.base;


import android.support.annotation.NonNull;

import com.jh3502.helper.utils.Constant;
import com.jh3502.helper.utils.eventbus.EventBusHelper;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public abstract class BaseCallback implements Callback<BaseJson> {

    private BaseView mBaseView;

    public BaseCallback(BaseView baseView) {
        mBaseView = baseView;
    }

    @Override
    public void onResponse(Call<BaseJson> call, @NonNull Response<BaseJson> response) {
        mBaseView.dismissLoadingDialog();
        BaseJson baseJson = response.body();
        EventBusHelper.handleBaseJson(baseJson);
        if (baseJson != null) {
            if (baseJson.getCode() != 0) {
                mBaseView.responseError(baseJson.getCode(), baseJson.getMsg());
            } else {
                responseSuccess(baseJson);
            }
        } else {
            mBaseView.responseError(Constant.UNKNOW_ERROR_CODE, Constant.UNKNOW_ERROR_MESSAGE);
        }
    }

    @Override
    public void onFailure(Call<BaseJson> call, Throwable t) {
        mBaseView.dismissLoadingDialog();
        mBaseView.requestFailure(t);
    }

    public abstract void responseSuccess(BaseJson baseJson);
}
